package com.myfojing.foshuoliaozhibingjing;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;

import static com.myfojing.foshuoliaozhibingjing.MainActivity.M_NEXTCLAUSE;
import static com.myfojing.foshuoliaozhibingjing.MainActivity.M_PROGRESS;


class XunfeiTTS extends AppCompatActivity {
    private SpeechSynthesizer mSpeechSynthesizer;
    private SynthesizerListener mSynListener;
    private InitListener mInitListener;

    Context context;
    Handler mHandler;

    boolean success;


    /* 构造函数 */
    public XunfeiTTS(Context context) {
        this.context = context;
        this.mHandler = ((MainActivity) context).handler;
        success = false;

        SpeechUtility.createUtility(context, SpeechConstant.APPID + "=586a0d9e");

    }

    public void initialTts() {
        if (success) return;
        // 初始化监听
        mInitListener = code -> {
            if (code != ErrorCode.SUCCESS) {
                Log.e("讯飞引擎：", "初始化失败,错误码：" + code);
            } else {
                // 合成参数设置，读取存储
                mSpeechSynthesizer.setParameter(SpeechConstant.VOLUME, "60"); //设置音量，范围0~100
                //mSpeechSynthesizer.setParameter(SpeechConstant.TEXT_ENCODING, "GB2312");
                mSpeechSynthesizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端
                mSpeechSynthesizer.setParameter(SpeechConstant.SPEED, "50");
                // 设置合成音频保存位置，保存在“./sdcard/iflytek.pcm”
                mSpeechSynthesizer.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm");
                success = true;
            }
        };

        // 创建SpeechSynthesizer对象, 第二个参数：本地合成时传InitListener
        mSpeechSynthesizer = SpeechSynthesizer.createSynthesizer(context, mInitListener);

        //合成监听器
        mSynListener = new SynthesizerListener() {
            //会话结束回调接口，没有错误时，error为null
            public void onCompleted(SpeechError error) {
                if (error == null) {
                    Message message = mHandler.obtainMessage();
                    message.what = M_NEXTCLAUSE;
                    mHandler.sendMessage(message);
                }
            }

            //缓冲进度回调
            //percent为缓冲进度0~100，beginPos为缓冲音频在文本中开始位置，endPos表示缓冲音频在文本中结束位置，info为附加信息。
            public void onBufferProgress(int percent, int beginPos, int endPos, String info) {
            }

            //开始播放
            public void onSpeakBegin() {

            }

            //暂停播放
            public void onSpeakPaused() {

            }

            //播放进度回调
            //percent为播放进度0~100,beginPos为播放音频在文本中开始位置，endPos表示播放音频在文本中结束位置.
            public void onSpeakProgress(int percent, int beginPos, int endPos) {
                Message message = mHandler.obtainMessage();
                message.what = M_PROGRESS;
                message.arg1 = percent;
                mHandler.sendMessage(message);

            }

            //恢复播放回调接口
            public void onSpeakResumed() {
                //
            }

            //会话事件回调接口
            public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
            }

        };
        // end
    }

    // 外部调用
    public void speak(String text) {
        if (mSpeechSynthesizer != null) {
            mSpeechSynthesizer.startSpeaking(text, this.mSynListener);
        }
    }

    public void pause() {
        if (mSpeechSynthesizer != null) {
            if (mSpeechSynthesizer.isSpeaking()) {
                mSpeechSynthesizer.pauseSpeaking();
            }
        }
    }

    public void resume() {
        if (mSpeechSynthesizer != null) {
            mSpeechSynthesizer.resumeSpeaking();
        }
    }

    public void stop() {
        if (mSpeechSynthesizer != null) {
            if (mSpeechSynthesizer.isSpeaking()) {
                mSpeechSynthesizer.stopSpeaking();
            }
        } // outer if
    }

    public void set_speed(int speed) {
        mSpeechSynthesizer.setParameter(SpeechConstant.SPEED, Integer.toString((speed + 10) / 10));  // +10
    }

    /* 设置发言人 */
    public void set_speaker(boolean speaker_is_male) {
        if (speaker_is_male)
            mSpeechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, "aisjiuxu");
        else
            mSpeechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, "aisjinger");
    }

    /* 设置音量 */
    public void set_volume(int volume) {
        if (volume < 0 || volume > 10) volume = 5;

        volume *= 10;
        if (mSpeechSynthesizer != null)
            mSpeechSynthesizer.setParameter(SpeechConstant.VOLUME, Integer.toString(volume));
    }

    /* 发送超时信号 */
    private void sendMsg(int msg) {
        Message message = mHandler.obtainMessage();
        message.what = msg;
        mHandler.sendMessage(message);
    }

    //
}
